package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.vo.ZhcaChartVo;
import com.ruoyi.system.bo.ZhcaChartQueryBo;
import com.ruoyi.system.bo.ZhcaChartAddBo;
import com.ruoyi.system.bo.ZhcaChartEditBo;
import com.ruoyi.system.service.IZhcaChartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图表Controller
 *
 * @author ruoyi
 * @date 2021-07-31
 */
@Validated
@Api(value = "图表控制器", tags = {"图表管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/chart")
public class ZhcaChartController extends BaseController {

    private final IZhcaChartService iZhcaChartService;

    /**
     * 查询图表列表
     */
    @ApiOperation("查询图表列表")
    @GetMapping("/list")
    public TableDataInfo<ZhcaChartVo> list(@Validated ZhcaChartQueryBo bo) {
        return iZhcaChartService.queryPageList(bo);
    }

	@ApiOperation("查询图表分组")
	@GetMapping("/groups")
    public AjaxResult<List<String>> groups(@RequestParam Integer type){
    	return AjaxResult.success(iZhcaChartService.selectGroups(type));
	}

	/**
	 * 导出图表列表
	 */
	@ApiOperation("导出图表列表")
	@Log(title = "图表", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<ZhcaChartVo> export(@Validated ZhcaChartQueryBo bo) {
		List<ZhcaChartVo> list = iZhcaChartService.queryList(bo);
		ExcelUtil<ZhcaChartVo> util = new ExcelUtil<ZhcaChartVo>(ZhcaChartVo.class);
		return util.exportExcel(list, "图表");
	}
	/**
	 * 导出图表列表
	 */
	@ApiOperation("导入图表列表")
	@Log(title = "图表", businessType = BusinessType.IMPORT)
	@PostMapping("/import")
	public AjaxResult impot(MultipartFile file) throws Exception {
		ExcelUtil<ZhcaChartVo> util = new ExcelUtil<ZhcaChartVo>(ZhcaChartVo.class);
		List<ZhcaChartVo> charts = util.importExcel(file.getInputStream());
		String message = iZhcaChartService.importCharts(charts);
		return AjaxResult.success(message);
	}

	@GetMapping("/importTemplate")
	public AjaxResult importTemplate()
	{
		ExcelUtil<ZhcaChartVo> util = new ExcelUtil<ZhcaChartVo>(ZhcaChartVo.class);
		return util.importTemplateExcel("图标数据");
	}

    /**
     * 获取图表详细信息
     */
    @ApiOperation("获取图表详细信息")
    @GetMapping("/{id}")
    public AjaxResult<ZhcaChartVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iZhcaChartService.queryById(id));
    }

    /**
     * 新增图表
     */
    @ApiOperation("新增图表")
    @Log(title = "图表", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody ZhcaChartAddBo bo) {
        return toAjax(iZhcaChartService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改图表
     */
    @ApiOperation("修改图表")
    @Log(title = "图表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody ZhcaChartEditBo bo) {
        return toAjax(iZhcaChartService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除图表
     */
    @ApiOperation("删除图表")
    @Log(title = "图表" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iZhcaChartService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
